﻿<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="../helpproject.xsl" ?>
<topic template="Default" lasteditedby="Geert" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../helpproject.xsd">
  <title translate="true">Introduction to MVVM and models</title>
  <body>
    <header>
      <para styleclass="Heading1"><text styleclass="Heading1" translate="true">Introduction to MVVM and models</text></para>
    </header>
    <para styleclass="Notes"><text styleclass="Notes" translate="true">If you haven&apos;t read the previous &quot;Getting started guides&quot; of Catel yet, it is recommended that you do. </text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">If you are not yet familiar with MVVM, please read: </text><link displaytype="text" defaultstyle="true" type="weblink" href="http://msdn.microsoft.com/en-us/magazine/dd419663.aspx" target="_blank" styleclass="Normal" translate="true">WPF Apps With the Model-View-ViewModel Design Pattern</link><text styleclass="Normal" translate="true"> by Josh Smith.</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">This part of the documentation will explain all the parts of MVVM, in the order in which we think they must be built. First of all, the Models, which are the closest to the business. Then, the View Models which define what part of the Models should be visible to the user in a specific situation. This also includes validation that is specific for the functionality that the View Model represents. Last, but not least, the View itself, which is the final representation of the View Model to the end-user.</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">As you will notice (or maybe you don&apos;t), this framework has a lot in common with other MVVM frameworks out there. This is normal because all of the frameworks are trying to implement the same pattern, which isn&apos;t that hard to understand if you think about it long enough. Before we started writing the MVVM framework, we first investigated other frameworks because there already are enough, probably too many. However, even the better (or best) still took too much time to use, and there was too much code we had to write to create a View Model. That&apos;s the point where we decided to write our own framework that contains lots of luxury for lazy developers such as us.</text></para>
    <para styleclass="Notes"><text styleclass="Notes" translate="true">When you want to be able to create View Models quickly, code snippets are the way to go. I can&apos;t say this enough, but you should really use code snippets; it saves you tons of type work.</text></para>
    <para styleclass="Heading1"><text styleclass="Heading1" translate="true">Models</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">The Models part is one of the three major parts of MVVM. Therefore, I want to tell you a bit about what kind of Models we use to store our data. Basically, you can use all types of objects as Models, as long as the Models implement the most commonly used interfaces required by WPF or Silverlight.</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">For MVVM, it is very important that the following interfaces are implemented:</text></para>
    <list id="3" type="ul" listtype="bullet" formatstring="&#183;" format-charset="SYMBOL_CHARSET" levelreset="true" legalstyle="false" startfrom="1" styleclass="Normal" style="font-family:Symbol; font-size:10pt; color:#000000;">
      <li styleclass="Normal"><text styleclass="Normal" style="font-weight:bold;" translate="true">INotifyPropertyChanged</text><br/><text styleclass="Normal" translate="true">If this interface is not implemented, changes will not be reflected to the UI via bindings. In other words, your Model and View Model will be useless in an MVVM setting.</text></li>
    </list>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">Finally, it is strongly recommended to have your Models implement the following interfaces as well:</text></para>
    <list id="4" type="ul" listtype="bullet" formatstring="&#183;" format-charset="SYMBOL_CHARSET" levelreset="true" legalstyle="false" startfrom="1" styleclass="Normal" style="font-family:Symbol; font-size:10pt; color:#000000;">
      <li styleclass="Normal"><text styleclass="Normal" style="font-weight:bold;" translate="true">IDataErrorInfo</text><br/><text styleclass="Normal" translate="true">If this interface is not implemented, errors cannot be shown to the user.</text></li>
      <li styleclass="Normal"><text styleclass="Normal" style="font-weight:bold;" translate="true">IEditableObject</text><br/><text styleclass="Normal" translate="true">If this interface is not implemented, a Model cannot work with “states”. This means that a user cannot start editing an object and finally cancel it (because there is no stored “state” that can be used to restore the values).</text></li>
    </list>
    <para styleclass="Heading2"><text styleclass="Heading2" translate="true">Database models</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">The most commonly used setting is an application that communicates with a database. There are tons of ORM tools out there, like the following:</text></para>
    <list id="5" type="ul" listtype="bullet" formatstring="&#183;" format-charset="SYMBOL_CHARSET" levelreset="true" legalstyle="false" startfrom="1" styleclass="Normal (list)" style="font-family:Symbol; font-size:10pt; color:#000000;">
      <li styleclass="Normal (list)"><link displaytype="text" defaultstyle="true" type="weblink" href="http://llblgen.com/defaultgeneric.aspx" target="_blank" styleclass="Normal (list)" translate="true">LLBLGen Pro</link><text styleclass="Normal (list)" translate="true">;</text></li>
      <li styleclass="Normal (list)"><link displaytype="text" defaultstyle="true" type="weblink" href="http://msdn.microsoft.com/en-us/library/aa697427(VS.80).aspx" target="_blank" styleclass="Normal (list)" translate="true">Entity Framework</link><text styleclass="Normal (list)" translate="true">;</text></li>
      <li styleclass="Normal (list)"><link displaytype="text" defaultstyle="true" type="weblink" href="http://nhforge.org/Default.aspx" target="_blank" styleclass="Normal (list)" translate="true">NHibernate</link><text styleclass="Normal (list)" translate="true">.</text></li>
    </list>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">I personally have a very good experience with LLBLGen Pro, but that&apos;s where I want to stop advertising. The most important thing is that the ORM mapper supports the interfaces that are required for WPF and Silverlight.</text></para>
    <para styleclass="Heading2"><text styleclass="Heading2" translate="true">File models</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">File models are really easy, assuming they are being used by a single user. Especially when you use the </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_Data_ModelBase" styleclass="Normal" translate="true">ModelBase</link><text styleclass="Normal" translate="true"> (or extended </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_Data_SavableModelBase" styleclass="Normal" translate="true">SavableodelBase</link><text styleclass="Normal" translate="true">) classes that also ship with Catel. A tree of objects can consist of objects. When this approach is used, it is very simple to take out a separate part and pass that as a Model to a View Model. Normally, you don&apos;t have to worry about multi-user environments like you have to when you use databases, so you can simply lock the file and you don&apos;t have to worry about concurrent updates.</text></para>
  </body>
</topic>
